This page introduces some commonly used methods to create arrays.
Let us start with an example. Suppose you want to create a row vector a, which contains 1, 2 and 3. Then, you can use a = [1 2 3]. Here, two numbers are separated by a whitespace, so that they are catenated horizontally. Instead of a whitespace, you can also use a comma. For example, a = [1, 2, 3] gives the same row vector.
When two numbers are separated by a semicolon, they are catenated vertically to form a column vector. For example, b = [1; 2; 3] is a column vector of 3 numbers, and d = [1, 3; 2 4] is a
The catenation method is not limited to numbers, it also works for arrays. In the following example, the c is obtained by vertically catenating a and b. Whereas, the row vector d is obtained by horizontally catenating a and b.
a = [1, 3];
b = [2, 4];
c = [a; b] % which gives a 2 x 2 array
d = [a b] % which gives [1 3 2 4]
c =
1.0000 3.0000
2.0000 4.0000
d =
1.0000 3.0000 2.0000 4.0000
Be sure that the arrays to be catenated horizontally (vertically) have the same number of rows (columns). Otherwise, a size-mismatch error will be thrown:
a = [1, 3] % 2 columns
b = [2; 4] % 1 columns
c = [a; b] % size mismatch.
a =
1.0000 3.0000
b =
2.0000
4.0000
Error at Line 4(5). Dimension lengths mismatch.
More illustrative examples are shown below.
[[], []] % gives an empty array
[2 []] % gives [2]
[[1 2 3 4]; [[5 6] 7 8]] % Nested catenation works!
ans =
[] (double array)
ans =
2.0000
ans =
1.0000 2.0000 3.0000 4.0000
5.0000 6.0000 7.0000 8.0000
The colon : operator and the function linspace are used frequently to create vectors.
Vectors of forms, such as : operator.
1:10
1:2:11
ans =
Columns 1 through 7:
1.0000 2.0000 3.0000 4.0000 5.0000 6.0000 7.0000
Columns 8 through 10:
8.0000 9.0000 10.000
ans =
1.0000 3.0000 5.0000 7.0000 9.0000 11.000
This is how the colon operator : works. Assume that b is positive and a < c. The statement a:b:c starts with a and keeps adding b to it until the sum reaches the biggest number not exceeding c.
In the following example, the last number in the vector is 9, not 11, because
1:2:10
ans =
1.0000 3.0000 5.0000 7.0000 9.0000
Assume that b is positive and a > c. The statement a:-b:c starts with a and keeps subtracting b from it until it is reaches the smallest number not smaller than c. For example,
10:-1:1
ans =
Columns 1 through 7:
10.000 9.0000 8.0000 7.0000 6.0000 5.0000 4.0000
Columns 8 through 10:
3.0000 2.0000 1.0000
The following vector excludes 1, because
10:-2:3
ans =
10.000 8.0000 6.0000 4.0000
The statement a:b:c gives an empty vector in the following cases:
b > 0 and a > cb < 0 and a < cb == 0For examples, each of the following gives an empty vector:
10:2:1
1:-1:10
1:0:10
linspaceWe use linspace(a,b,N) to create a vector of N numbers ranging from a to b. If N is not specified, it returns a vector of 100 numbers.
Here are some examples:
% A vector of 10 number in ascending order.
linspace(1,2,10)
% A vector of 11 numbers in descending order.
linspace(2,1,11)
ans =
Columns 1 through 7:
1.0000 1.1111 1.2222 1.3333 1.4444 1.5556 1.6667
Columns 8 through 10:
1.7778 1.8889 2.0000
ans =
Columns 1 through 7:
2.0000 1.9000 1.8000 1.7000 1.6000 1.5000 1.4000
Columns 8 through 11:
1.3000 1.2000 1.1000 1.0000
More specifically, linspace(a,b,n) returns the vector
For more details, see the document page for linspace.
The following functions create constant arrays. For more details, see the respective document pages.
ones: Array of ones.zeros: Array of zeros.eye: Identity matrix.true: Array of logical true values.false: Array of logical false values.inf: Array of nan: Array of nan.pi: Scalar approximating blanks: Array of white-spaces.The following functions create arrays of random numbers. For more details, see the respective document pages.
randi: Array of positive random integers.rand: Array of uniformly distributed random numbers lying in the interval randn: Array of normally distributed random numbers.normrnd: Array of random numbers following the normal distribution of given mean and standard deviation.poissrnd: Array of random numbers following the Poisson distribution of a given mean.exprnd: Array of random numbers following the exponential distribution of a given mean.